package com.herch.system.service.impl;

import com.herch.common.constant.MnlConstans;
import com.herch.common.utils.CommonUtil;
import com.herch.system.domain.HqMonitorData;
import com.herch.system.domain.HqMonitorDataIntermediate;
import com.herch.system.domain.HqParamSet;
import com.herch.system.mapper.*;
import com.herch.system.service.IFZ420DataAcquisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DependsOn("springContextUtil")
public class FZ420DataAcquisitionServiceImpl implements IFZ420DataAcquisitionService {
	@Autowired
	private HqParamSetMapper paramSetMapper;
	@Autowired
	private HqMonitorDataIntermediateMapper monitorDataIntermediateMapper;
	@Autowired
	private HqMonitorDataMapper monitorDataMapper;
	@Autowired
	private SimpMessagingTemplate simpMessagingTemplate;
	@Override
	public void parseFZ420ReceivedData(String inputString) {
		String receiveTime = CommonUtil.getSimpleFormatTimestamp();
		Map<String,Object> map = new HashMap<>();
		// 地址编码
		String address = Integer.valueOf(inputString.substring(0, 2), 16).toString();
		//查询设备最大时间监测数据，即设备最新的数据
		List<HqMonitorDataIntermediate> maxTimeMonitorData = monitorDataIntermediateMapper.selectMaxTimeData(address);;
		map.put("fz420ReceiveData",maxTimeMonitorData.get(0).getEquipmentName()+"<<<"+inputString);
		simpMessagingTemplate.convertAndSend( "/data/task", map);
		// 通道数
		inputString = inputString.substring(6);
		List<HqParamSet> paramSetList =  paramSetMapper.selectHqParamSetList();
		Double minTem = Double.valueOf(paramSetList.get(0).getThresholdValue());
		Double maxTem = Double.valueOf(paramSetList.get(1).getThresholdValue());
		Double minAV = Double.valueOf(paramSetList.get(2).getThresholdValue());
		Double maxAV = Double.valueOf(paramSetList.get(3).getThresholdValue());
		Double minMnl = Double.valueOf(paramSetList.get(4).getThresholdValue());
		Double maxMnl = Double.valueOf(paramSetList.get(5).getThresholdValue());
		Double errorVal = Double.valueOf(paramSetList.get(6).getThresholdValue());
		double avValue = 0d;
		double temValue = 0d;
		int i = 0;
		for(HqMonitorDataIntermediate monitorMiddleData: maxTimeMonitorData){
			if(address.equals(monitorMiddleData.getEquipmentAddr()) && ("CH"+(i+1)).equals(monitorMiddleData.getChannelNum())){
				HqMonitorData monitorData = new HqMonitorData();
				//获取每个通道的16进制字符串
				String chanelStr = inputString.substring(i * 4, i * 4 + 4).toLowerCase();
				//将16进制转为整型的数码值
				int mnlVal = Integer.parseInt(chanelStr, 16);
				if((maxMnl-minMnl)!=0){
					//计算电流/电压值
					avValue = ((mnlVal-minMnl)*(maxAV-minAV)/(maxMnl-minMnl))+ minAV;
					//计算温度值
					temValue = ((mnlVal-minMnl)*(maxTem-minTem)/(maxMnl-minMnl))+ minTem;
				}
				monitorData.setEquipmentId(monitorMiddleData.getEquipmentId());
				monitorData.setEquipmentName(monitorMiddleData.getEquipmentName());
				monitorData.setEquipmentSize(monitorMiddleData.getEquipmentSize());
				monitorData.setNumOfCh(monitorMiddleData.getNumOfCh());
				monitorData.setEquipmentAddr(address);
				monitorData.setChannelNum("CH"+(i+1));
				monitorData.setTemperature(monitorMiddleData.getTemperature());
				monitorData.setPd(monitorMiddleData.getPd());
				monitorData.setUv(monitorMiddleData.getUv());
				monitorData.setTemState(monitorMiddleData.getTemState());
				monitorData.setStateName(monitorMiddleData.getStateName());
				monitorData.setAnalog(String.format("%.2f", avValue));
				monitorData.setSimulationTemperature(String.format("%.2f", temValue));
				Double discrepancy = temValue-Double.valueOf(monitorData.getTemperature());
				String temError = String.format("%.2f",discrepancy);
				monitorData.setTemperatureError(temError);
				String errorResult = Math.abs(discrepancy)  > errorVal ? "不合格" :"合格";
				monitorData.setErrorResult(errorResult);
				monitorData.setCollectTime(receiveTime);
				//讲临时表中查询的数据插入到各设备的监测表中
				updateMonitorDataByT(address, monitorData);
				Map<String,Object> middleMap = new HashMap<>();
				middleMap.put("receiveTime",monitorMiddleData.getCollectTime());
				middleMap.put("address",address);
				//根据当前中间表采集时间和设备地址清除临时表中的数据
				monitorDataIntermediateMapper.deleteHqMonitorDataIntermediate(middleMap);
				i++;
			}
		}
	}

	private void updateMonitorDataByT(String address, HqMonitorData monitorData) {
		switch(address){
			case MnlConstans.T1:
				monitorData.setTableName(MnlConstans.Tab_1);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T2:
				monitorData.setTableName(MnlConstans.Tab_2);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T3:
				monitorData.setTableName(MnlConstans.Tab_3);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T4:
				monitorData.setTableName(MnlConstans.Tab_4);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T5:
				monitorData.setTableName(MnlConstans.Tab_5);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T6:
				monitorData.setTableName(MnlConstans.Tab_6);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T7:
				monitorData.setTableName(MnlConstans.Tab_7);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T8:
				monitorData.setTableName(MnlConstans.Tab_8);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T9:
				monitorData.setTableName(MnlConstans.Tab_9);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T10:
				monitorData.setTableName(MnlConstans.Tab_10);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T11:
				monitorData.setTableName(MnlConstans.Tab_11);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T12:
				monitorData.setTableName(MnlConstans.Tab_12);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T13:
				monitorData.setTableName(MnlConstans.Tab_13);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T14:
				monitorData.setTableName(MnlConstans.Tab_14);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T15:
				monitorData.setTableName(MnlConstans.Tab_15);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T16:
				monitorData.setTableName(MnlConstans.Tab_16);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T17:
				monitorData.setTableName(MnlConstans.Tab_17);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T18:
				monitorData.setTableName(MnlConstans.Tab_18);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T19:
				monitorData.setTableName(MnlConstans.Tab_19);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T20:
				monitorData.setTableName(MnlConstans.Tab_20);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T21:
				monitorData.setTableName(MnlConstans.Tab_21);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T22:
				monitorData.setTableName(MnlConstans.Tab_22);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T23:
				monitorData.setTableName(MnlConstans.Tab_23);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T24:
				monitorData.setTableName(MnlConstans.Tab_24);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T25:
				monitorData.setTableName(MnlConstans.Tab_25);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T26:
				monitorData.setTableName(MnlConstans.Tab_26);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T27:
				monitorData.setTableName(MnlConstans.Tab_27);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T28:
				monitorData.setTableName(MnlConstans.Tab_28);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T29:
				monitorData.setTableName(MnlConstans.Tab_29);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T30:
				monitorData.setTableName(MnlConstans.Tab_30);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T31:
				monitorData.setTableName(MnlConstans.Tab_31);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T32:
				monitorData.setTableName(MnlConstans.Tab_32);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T33:
				monitorData.setTableName(MnlConstans.Tab_33);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T34:
				monitorData.setTableName(MnlConstans.Tab_34);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T35:
				monitorData.setTableName(MnlConstans.Tab_35);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T36:
				monitorData.setTableName(MnlConstans.Tab_36);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T37:
				monitorData.setTableName(MnlConstans.Tab_37);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T38:
				monitorData.setTableName(MnlConstans.Tab_38);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T39:
				monitorData.setTableName(MnlConstans.Tab_39);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T40:
				monitorData.setTableName(MnlConstans.Tab_40);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T41:
				monitorData.setTableName(MnlConstans.Tab_41);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T42:
				monitorData.setTableName(MnlConstans.Tab_42);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T43:
				monitorData.setTableName(MnlConstans.Tab_43);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T44:
				monitorData.setTableName(MnlConstans.Tab_44);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T45:
				monitorData.setTableName(MnlConstans.Tab_45);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T46:
				monitorData.setTableName(MnlConstans.Tab_46);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T47:
				monitorData.setTableName(MnlConstans.Tab_47);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T48:
				monitorData.setTableName(MnlConstans.Tab_48);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T49:
				monitorData.setTableName(MnlConstans.Tab_49);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T50:
				monitorData.setTableName(MnlConstans.Tab_50);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T51:
				monitorData.setTableName(MnlConstans.Tab_51);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T52:
				monitorData.setTableName(MnlConstans.Tab_52);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T53:
				monitorData.setTableName(MnlConstans.Tab_53);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T54:
				monitorData.setTableName(MnlConstans.Tab_54);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T55:
				monitorData.setTableName(MnlConstans.Tab_55);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T56:
				monitorData.setTableName(MnlConstans.Tab_56);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T57:
				monitorData.setTableName(MnlConstans.Tab_57);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T58:
				monitorData.setTableName(MnlConstans.Tab_58);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T59:
				monitorData.setTableName(MnlConstans.Tab_59);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
			case MnlConstans.T60:
				monitorData.setTableName(MnlConstans.Tab_60);
				monitorDataMapper.insertHqMonitorData(monitorData);
				break;
		}
	}
}